-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: enhance tool use instructions for Ollama models (#8430) #8431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add enhanced XML formatting instructions for local models (Ollama/LMStudio) - Include explicit examples of correct and incorrect tool usage - Add mandatory requirements to prevent models from adding explanatory text - Pass apiProvider through system prompt generation chain - Add comprehensive tests for the new functionality This addresses the issue where local Ollama models were not properly following the XML tool format, causing them to hallucinate code or enter infinite loops instead of using the codebase_search tool. Fixes #8430
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self-review: auditing my own prompt changes like a linter staring into a mirror—merciless and oddly recursive.
| // Enhanced instructions for local models that may struggle with tool formatting | ||
| const isLocalModel = apiProvider === "ollama" || apiProvider === "lmstudio" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Normalize provider for robustness and accept common alias. This avoids missing enhanced instructions when the provider value differs in case or includes a hyphen (e.g., 'LMStudio', 'lm-studio'). Suggested minimal change below:
| // Enhanced instructions for local models that may struggle with tool formatting | |
| const isLocalModel = apiProvider === "ollama" || apiProvider === "lmstudio" | |
| // Enhanced instructions for local models that may struggle with tool formatting | |
| const provider = (apiProvider ?? '').toLowerCase() | |
| const isLocalModel = provider === 'ollama' || provider === 'lmstudio' || provider === 'lm-studio' |
| expect(result).toContain("Remember: Your ENTIRE response should be the tool XML, nothing else") | ||
| }) | ||
|
|
||
| it("should include enhanced instructions for lmstudio provider", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Consider adding test coverage for provider case/alias normalization (e.g., 'LMStudio' and 'lm-studio') to prevent regressions. Example to add near the existing lmstudio test:
it('should include enhanced instructions for LMStudio (case-insensitive and alias)', () => {
const result1 = getSharedToolUseSection('LMStudio')
const result2 = getSharedToolUseSection('lm-studio')
;[result1, result2].forEach((result) => {
expect(result).toContain('CRITICAL: Tool Use Requirements for Your Response')
expect(result).toContain('Every response MUST contain EXACTLY ONE tool use')
})
})| }, | ||
| undefined, // todoList | ||
| undefined, // modelId | ||
| apiConfiguration?.apiProvider, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: Optional: If provider normalization is handled in src/core/prompts/sections/tool-use.ts, no action needed here. Otherwise, consider normalizing apiConfiguration?.apiProvider at the call-site to reduce duplication.
Summary
This PR fixes an issue where local Ollama/LMStudio models were not properly calling the codebase_search tool, instead hallucinating code content and entering infinite loops.
Problem
As reported in #8430, when using Ollama with local models (specifically llama3.1:8b-instruct-q4_K_M), the LLM:
Solution
The fix adds enhanced, explicit tool use instructions specifically for local models (Ollama and LMStudio) that struggle with the XML-formatted tool calls. The solution:
Detects local models by checking if apiProvider is 'ollama' or 'lmstudio'
Provides stricter instructions with:
Maintains backward compatibility - other providers continue to receive the standard instructions
Changes
src/core/prompts/sections/tool-use.tsto add conditional enhanced instructionssrc/core/prompts/system.tssrc/core/task/Task.tssrc/core/webview/generateSystemPrompt.tssrc/core/prompts/sections/__tests__/tool-use.spec.tsTesting
Impact
This change should significantly improve the reliability of tool usage for users running local Ollama or LMStudio models, preventing the hallucination and infinite loop issues described in the bug report.
Fixes #8430
Important
Enhanced tool use instructions for local models in
tool-use.ts, ensuring mandatory tool use and providing examples, with changes propagated through the prompt generation chain and tested intool-use.spec.ts.ollamaandlmstudio) intool-use.ts.apiProviderintool-use.ts.tool-use.tsto add conditional instructions based onapiProvider.system.ts,Task.ts, andgenerateSystemPrompt.tsto passapiProviderthrough the prompt generation chain.tool-use.spec.tsto cover enhanced instructions for local models.This description was created by
for 7c29ffb. You can customize this summary. It will automatically update as commits are pushed.